package sunstar.com.xq.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import sunstar.com.xq.R;

/**
 * Created by Sun.Star on 2017/6/22.
 */

public class VopenMenu extends ViewGroup {

    private List<LinearLayout> buttonList = new ArrayList<>();
    private int bgColor;
    private ImageView baseButton;
    private int baseImgId;


    public VopenMenu(Context context) {
        this(context,null);
    }

    public VopenMenu(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public VopenMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MenuBtn,defStyleAttr,0);
        bgColor = array.getColor(R.styleable.MenuBtn_bgColor,R.color.colorPrimaryDark);
        baseImgId = array.getResourceId(R.styleable.MenuBtn_menuImg,0);
        baseButton = new ImageView(context);
        baseButton.setImageResource(baseImgId);
        baseButton.setLayoutParams(new LayoutParams(100,100));
        baseButton.setPadding(10,10,10,10);
        baseButton.setBackground(context.getDrawable(R.drawable.v_open_menu));
    }

    public void addMenu(int imgId,String text){
        LinearLayout linearLayout = new LinearLayout(getContext());
        TextView tv = new TextView(getContext());
        ImageView iv = new ImageView(getContext());
        tv.setText(text);
        iv.setImageResource(imgId);
        linearLayout.setPadding(0,0,0,10);
        iv.setBackground(getContext().getDrawable(R.drawable.v_open_menu));
        GradientDrawable drawable = (GradientDrawable) iv.getBackground();
        drawable.setColor(getResources().getColor(bgColor));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(iv);
        linearLayout.addView(tv);
        buttonList.add(linearLayout);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        for (int i = 0; i <buttonList.size() ; i++) {
            measureChild(buttonList.get(i),widthMeasureSpec,heightMeasureSpec);
        }
        measureChild(baseButton,widthMeasureSpec,heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        removeAllViews();
        addView(baseButton);
    }
}
